<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentIndex: 0,
      timerId: null,
      titleFontSize: 0,
    }
  },
  computed: {
    ...mapState(['theme']),
  },
  watch: {
    theme() {
      this.chartInstance.dispose()
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    },
  },
  created() {
    this.$socket.registerCallBack('stockData', this.getData)
  },
  mounted() {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
    })
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
    this.$socket.unRegisterCallBack('stockData')
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref, this.theme)
      const initOption = {
        title: { text: '▎ 库存销售量', left: 20, top: 20 },
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      const { data: res } = await this.$http.get('stock')
      this.allData = res
      this.updateChart()
      this.startInterval()
    },
    updateChart() {
      const centerPointers = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%'],
      ]
      const colorArrs = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF'],
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesStocks = showData.map((item, index) => {
        return {
          // type: 'pie', // 为啥这里写type不行？
          hoverAnimation: false,
          labelLine: {
            show: false,
          },
          label: {
            show: true,
            position: 'center',
            color: colorArrs[index][0],
          },
          center: centerPointers[index],
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 1,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: colorArrs[index][0],
                    },
                    {
                      offset: 1,
                      color: colorArrs[index][1],
                    },
                  ],
                },
              },
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843',
              },
            },
          ],
        }
      })
      const updateOption = {
        series: seriesStocks,
      }
      this.chartInstance.setOption(updateOption)
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6
      const innerRaidus = this.titleFontSize * 3
      const outterRadius = innerRaidus * 1.125
      // 也可以用 Array.from({ length: 5 })
      const series = Array.apply(null, { length: 5 }).map(() => {
        return {
          type: 'pie',
          radius: [outterRadius, innerRaidus],
          label: {
            fontSize: this.titleFontSize / 1.3,
          },
        }
      })
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        series: series,
      }
      console.log('series', series)
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > Math.floor(this.allData.length / 5) - 1) {
          this.currentIndex = 0
        }
        this.updateChart()
      }, 3000)
    },
  },
}
</script>

<style></style>
